package com.crazymaker.agent.demo.asm;

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;

import java.lang.instrument.ClassFileTransformer;
import java.security.ProtectionDomain;

public class Transformer implements ClassFileTransformer {

    @Override
    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)  {

        byte[] bytes = null;
        if (className.equals("com/crazymaker/circle/agent/Helloworld")) {
            try {
                ClassReader reader = new ClassReader(className);

                ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS);
                ClassVisitor classVisitor = new HelloworldClassVisitor(writer);
                reader.accept(classVisitor, ClassReader.SKIP_DEBUG);

                bytes = writer.toByteArray();
            } catch (Exception e) {
            }
        } else {
        }

        return bytes;
    }

}